home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 440_01 / examples / ex_6.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-16  |  9.5 KB  |  193 lines

  1. #include <alloc.h>
  2. #include <stdlib.h>
  3. #include <graphics.h>
  4. #include "colors.h"
  5. #include "!bestlib.h"
  6.  
  7. #define BGCOLOR BLACK                  /* background color                  */
  8.  
  9.    /*** NOTE  the names and structure of the routines used in these examples
  10.               have changed in The Best Library 2.00; the updated examples will
  11.               be released soon                                            ***/
  12.  
  13.    /*** NOTE  even though this program only uses the "keyp" and "msdata"
  14.               structures, the other two are necessary for the assembler
  15.               routines in the !BESTLIB.LIB library to function properly.  C
  16.               would also produce a "linker warning" if you have enabled that
  17.               warning                                                     ***/
  18. filldata fidata;                       /* create a "filldata" structure     */
  19. printdata prdata;                      /* create a "printdata" structure    */
  20. mousedata msdata;                      /* create a "mousedata" structure    */
  21. asciiscan keyp;                        /* create an "asciiscan" structure   */
  22.  
  23. void main(void)
  24. {
  25.    typedef struct {
  26.       int x;
  27.       int y;
  28.       int maxx;
  29.       int maxy;
  30.    } graphicobject;
  31.  
  32.    graphicobject *graphic;
  33.    int oldmode, mouse, gdrv = VGA, gmod = VGAHI;
  34.    register int i, ii;
  35.    void *bgscrn[3], *image_or[1], *image[3];
  36.  
  37.    if (coreleft() < 280000L) {         /* if we do not have enough memory.. */
  38.       textm(0);                        /* make sure we are in a text mode   */
  39.       printsatcur("Insufficient memory to run EXAMP006.EXE");
  40.       exit(1);                         /* exit to DOS with errorlevel 1     */
  41.    }
  42.    if (isitega() == FALSE) {
  43.       textm(0);                        /* make sure we are in a text mode   */
  44.       printsatcur("An EGA/VGA card is required to run EXAMP006.EXE");
  45.       exit(2);                         /* exit to DOS with errorlevel 2     */
  46.    }
  47.    if ((oldmode = readvideomode()) == 3 || oldmode == 7 || oldmode == 21) {
  48.                                        /* if the old mode was a text mode.. */
  49.       textmem(3);                      /* store text video memory           */
  50.       cursor(3, 1);                    /* hide and store cursor position    */
  51.    }
  52.    initgraph(&gdrv, &gmod, "");        /* initialize graphics mode          */
  53.    settextjustify(LEFT_TEXT, TOP_TEXT);
  54.    settextstyle(SMALL_FONT, HORIZ_DIR, 4);         /* setup font size/style */
  55.  
  56.    mouse = initms_gr();             /* initialize mouse driver if installed */
  57.    floodall(BGCOLOR);                  /* create the screen background      */
  58.  
  59.    for (i = 0; i < 5; i++)             /* create the screen border          */
  60.       boxoutline(i, i, MAXX - i*2, MAXY - i*2, YELLOW, COPY_IMAGE);
  61.  
  62.                                        /* create the screen background      */
  63.    for (i = BLACK, ii = 5; i <= WHITE; i++, ii += 21)
  64.       boxfill(ii, 5, 21, MAXY - 10, i, COPY_IMAGE);
  65.    for (i = YELLOW; i > BLACK; i--, ii += 21)
  66.       boxfill(ii, 5, 21, MAXY - 10, i, COPY_IMAGE);
  67.  
  68.             /* print messages showing user each animation routine in action */
  69.    boxfill(5, 36, 50, 12, BLACK, COPY_IMAGE);
  70.    outtextxy(5, 36, "animate()");
  71.    boxfill(5, 136, 57, 12, BLACK, COPY_IMAGE);
  72.    outtextxy(5, 136, "animate1()");
  73.    boxfill(5, 236, 78, 12, BLACK, COPY_IMAGE);
  74.    outtextxy(5, 236, "animatepixel()");
  75.    boxfill(5, 336, 85, 12, BLACK, COPY_IMAGE);
  76.    outtextxy(5, 336, "animatebestss()");
  77.  
  78.                                        /* store the background into memory  */
  79.    bgscrn[0] = (void *) malloc(imagememreq(640, 204));
  80.    bgscrn[1] = (void *) malloc(imagememreq(640, 204));
  81.    bgscrn[2] = (void *) malloc(imagememreq(640, 72));
  82.    storeimage(0, 0, 640, 204, bgscrn[0]);
  83.    storeimage(0, 204, 640, 204, bgscrn[1]);
  84.    storeimage(0, 408, 640, 72, bgscrn[2]);
  85.  
  86.                              /* allocate memory for graphicobject structure */
  87.    graphic = (graphicobject *) malloc(sizeof(graphicobject));
  88.  
  89.                  /* draw the image to animate outside the visible video RAM */
  90.  /* because it is outside the visible range, mouse cursor can stay onscreen */
  91.    #define CLR1 LIGHTBLUE
  92.    #define CLR2 YELLOW                         /* draw the true-color image */
  93.    boxfill(0, MAXY, 32, 32, BGCOLOR, COPY_IMAGE);
  94.    boxfill(13, MAXY, 7, 32, CLR1, COPY_IMAGE);
  95.    boxfill(0, MAXY+13, 32, 7, CLR1, COPY_IMAGE);
  96.    linexy(16, MAXY, 31, MAXY+15, CLR2);
  97.    linexy(31, MAXY+15, 15, MAXY+31, CLR2);
  98.    linexy(15, MAXY+31, 0, MAXY+16, CLR2);
  99.    linexy(0, MAXY+16, 16, MAXY, CLR2);
  100.    boxfill(12, MAXY+12, 9, 9, BLACK, COPY_IMAGE);
  101.  
  102.    image[0] = (void *) malloc(imagememreq(32, 32));       /* for storeimage */
  103.    image_or[0] = (void *) malloc(imagememreq(32, 32));    /* for storeimage */
  104.    image[1] = (void *) malloc(pixelimagememreq(32, 32));
  105.                                                      /* for storepixelimage */
  106.    image[2] = (void *) malloc(pixelrcompressimagememreq(0, MAXY, 32, 32));
  107.                                              /* for storepixelcompressimage */
  108.    storeimage(0, MAXY, 32, 32, image[0]);                     /* save image */
  109.    storepixelimage(0, MAXY, 32, 32, BGCOLOR, image[1]);       /* save image */
  110.    storepixelcompressimage(0, MAXY, 32, 32, BGCOLOR, image[2]); /* sv image */
  111.  
  112.             /* draw the graphics to animate outside the visible screen area */
  113.    #undef CLR1
  114.    #undef CLR2
  115.    #define CLR1 WHITE
  116.    #define CLR2 WHITE                          /* draw the pure-white image */
  117.    boxfill(0, MAXY, 32, 32, BGCOLOR, COPY_IMAGE);
  118.    boxfill(13, MAXY, 7, 32, CLR1, COPY_IMAGE);
  119.    boxfill(0, MAXY+13, 32, 7, CLR1, COPY_IMAGE);
  120.    linexy(16, MAXY, 31, MAXY+15, CLR2);
  121.    linexy(31, MAXY+15, 15, MAXY+31, CLR2);
  122.    linexy(15, MAXY+31, 0, MAXY+16, CLR2);
  123.    linexy(0, MAXY+16, 16, MAXY, CLR2);
  124.    boxfill(12, MAXY+12, 9, 9, BLACK, COPY_IMAGE);
  125.    storeimage(0, MAXY, 32, 32, image_or[0]);   /* save the pure-white image */
  126.  
  127.                             /* display all the graphics -- ready to animate */
  128.    writeimage(6, 50, COPY_IMAGE, image[0]);
  129.    writeimage(6, 150, COPY_IMAGE, image[0]);
  130.    writepixelimage(6, 250, image[1]);
  131.    writepixelcompressimage(6, 350, image[2]);
  132.    if (mouse == TRUE) showms();        /* if mouse detected, show ms cursor */
  133.    getchr();    /* wait for a keypress before beginning animation sequences */
  134.    graphic->maxx = graphic->maxy = 32; /* initialize the graphic dimensions */
  135.    graphic->x = 6, graphic->y = 50;   /* initialize graphic coordinates    */
  136.    if (mouse == TRUE) hidems();        /* if mouse detected, hide ms cursor */
  137.  
  138.    for (i = 6; i < MAXX - 37; graphic->x = i, i += 2) {
  139.       if (keyhit()) {                  /* if the user hit a key..           */
  140.          getchr();                     /* read the key                      */
  141.          if (keyp.ascii == 27)         /* if it was the <ESC> key..         */
  142.             exit(0);                   /* exit to DOS with errorlevel 0     */
  143.          break;                       /* else abort this animation sequence */
  144.       }
  145.       animate((int *)graphic, i, 50, 0, image_or, image, bgscrn);
  146.    }
  147.  
  148.    graphic->x = 6, graphic->y = 150;
  149.    for (i = 6; i < MAXX - 37; graphic->x = i, i += 2) {
  150.       if (keyhit()) {                  /* if the user hit a key..           */
  151.          getchr();                     /* read the key                      */
  152.          if (keyp.ascii == 27)         /* if it was the <ESC> key..         */
  153.             exit(0);                   /* exit to DOS with errorlevel 0     */
  154.          break;                       /* else abort this animation sequence */
  155.       }
  156.       animate1((int *)graphic, i, 150, 0, image, bgscrn);
  157.    }
  158.  
  159.    graphic->x = 6, graphic->y = 250;
  160.    for (i = 6; i < MAXX - 37; graphic->x = i, i += 2) {
  161.       if (keyhit()) {                  /* if the user hit a key..           */
  162.          getchr();                     /* read the key                      */
  163.          if (keyp.ascii == 27)         /* if it was the <ESC> key..         */
  164.             exit(0);                   /* exit to DOS with errorlevel 0     */
  165.          break;                       /* else abort this animation sequence */
  166.       }
  167.       animatepixel((int *)graphic, i, 250, 1, image, bgscrn);
  168.    }
  169.  
  170.    graphic->x = 6;
  171.    for (i = 6; i < MAXX - 37; graphic->x = i, i += 2) {
  172.       if (keyhit()) {                  /* if the user hit a key..           */
  173.          getchr();                     /* read the key                      */
  174.          if (keyp.ascii == 27)         /* if it was the <ESC> key..         */
  175.             exit(0);                   /* exit to DOS with errorlevel 0     */
  176.          break;                       /* else abort this animation sequence */
  177.       }
  178.       animatebestss(graphic->x, 350, i, 350, image[2], image[2], bgscrn);
  179.    }
  180.  
  181.    free(image_or[0]);                  /* free all allocated memory         */
  182.    for (i = 0; i < 3; i++) free(image[i]);
  183.    if (mouse == TRUE) showms();        /* if mouse detected, show ms cursor */
  184.    getchr();                           /* wait for keypress before exitting */
  185.    if (mouse == TRUE) hidems();        /* if mouse detected, hide ms cursor */
  186.    changevideomode(oldmode);           /* restore the original video mode   */
  187.    if (oldmode == 3 || oldmode == 7 || oldmode == 21) {
  188.                                        /* if the old mode was a text mode.. */
  189.       textmem(1);                      /* restore text video memory         */
  190.       cursor(1, 1);                    /* show and restore cursor position  */
  191.    }
  192. }
  193.